import { ResultSetHeader } from "mysql2";
import { getDB } from "~/server/utils/db/mysql/index";
import { responseJson } from "~/server/utils/helper/index";

export default defineEventHandler(async (event) => {
  const params = await getQuery(event);

  // 设置默认值
  const page = params.page ? Number(params.page) : 1;
  const pageSize = params.pageSize ? Number(params.pageSize) : 10;

  const con = getDB();

  try {
    const [rows] = await con.query<ResultSetHeader>(
      "SELECT * FROM `notes` LIMIT ? OFFSET ?",
      [pageSize, (page - 1) * pageSize]
    );
    return responseJson(0, "获取成功", {
      list: rows,
    });
  } catch (error) {
    console.error("获取文章失败:", error);
    return responseJson(1, "获取失败", {});
  }
  // 注意：不要关闭连接池！连接池应该保持打开状态以供后续使用
  // 连接池会自动管理连接的获取和释放
});
